
global name "B0_ui_prepare_weights"
cap log close
log using "${pfile}/${name}.smcl", replace smcl

global var "cash sur3 sur6 sur36 dur6 dur36 n_remmedr n_remdezr n_remmedia n_jdur n_hor manager whitec migocup migind mig miguf totmon mon12 mon24 mon36 totwage wage12 wage24 wage36 totinc inc12 inc24 inc36" //

global mon_i = 1         // Workers dismissed from this month
global mon_f = 12        // Workers dismissed until this month
global cause = 11
global ten_min = 14   // minimum tenure at lay-off in months
global ten_max = 32   // maximum tenure at lay-off in months

// reweight sample to make it comparable to cash grant sample
// based on area of the country, years, tenure, and wage.

use "A0_cash_main.dta", clear

* merging info on duration
merge 1:1 year id using A0_additional, keep(1 3) nogen keepus(n_und premon12 premon24 premon36)
replace n_und = (mdy(12,31,2017) - dt_dism)/7 if n_und==.
replace und = n_und
drop n_und

replace premon12 = 0 if premon12==.
replace premon24 = 0 if premon24==.
replace premon36 = 0 if premon36==.
g pretotmon = premon12+premon24+premon36


keep if abs(x)<=.4

drop if x==0
// Sample selection
drop if dt_dism < mdy(6,1,year)
drop if dt_dism > mdy(7,15,year)
keep if tempempr>=16 & tempempr<=30 // similar tenure to cash ui extension sample

g samplecash = 1

keep samplecash year id cpf school remmedr remmedia tempempr dt_dism dtb race gend school uf ind bon_inf ben_inf und
replace school = school*100
append using "B0_ui.dta"
replace samplecash = 0 if samplecash==.
drop if (remmedia<1.6 | remmedia>2.4) & samplecash==0
drop if (year<=2010 | year>=2015) & samplecash==0

cap drop age_dism
g age_dism = (dt_dism-dtb)/365
probit samplecash remmedr tempempr age_dism i.race gend i.school i.uf i.ind
predict proba

cap drop weight
g weight=proba/(1-proba)
replace weight = 0 if weight == . // 0 weight to observations from different years from the cash grant
replace weight = 1 if samplecash==1

g reg = int(uf/10)
tab reg, g(reg)

g serv = ind==6
g sales = ind==5
g construc = ind==4
g manu = ind==3

g white=race==2
g mixed=race==8
g black=race==4

* merging info on duration
merge m:1 year id using B0_additional, keep(1 3) nogen keepus(n_und)
replace n_und = (mdy(12,31,2017) - dt_dism)/7 if n_und==. & samplecash==0
replace und = n_und if samplecash==0


ebalance samplecash remmedr tempempr age_dism i.race gend i.school i.uf i.ind, g(w_ebal) tar(1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2)

foreach x in 5 6 36 { // everything monthly
g sur`x' = und > 30/7 * (`x')
g dur`x' = und
replace dur`x' = 30/7 * `x' if und >= 30/7 * `x'
}

replace school = school/100
gstats tab remmedia tempempr age_dism gend school white black mixed reg1-reg5 serv sales constr manu ben_inf bon_inf sur5 sur6 dur6 dur36 if samplecash==0, by(samplecash) stats(mean sd)
gstats tab remmedia tempempr age_dism gend school white black mixed reg1-reg5 serv sales constr manu ben_inf bon_inf sur5 sur6 dur6 dur36 [w=w_ebal], by(samplecash) stats(mean sd)

stddiff remmedia tempempr age_dism gend school white black mixed reg1-reg5 serv sales constr manu ben_inf bon_inf sur5 sur6 dur6 dur36 , by(samplecash)

ta samplecash
keep if samplecash==0

su weight, d

keep year id weight w_ebal

save B0_weights, replace
*/
